﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Fly.Common.WorkFlow;
using Telerik.Web.UI;
using Telerik.Web.UI.Skins;
using System.Configuration;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseCorpListClear : BasePage
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }

        protected string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFlowCode();
               
            }
        }

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }

        }
        protected void ListCorps_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            var queryCondition = GetCondition();

            var corp = enterpriseServiceClient.GetCompanyAllInfo(queryCondition, ListCorps.PageSize, ListCorps.CurrentPageIndex + 1, UserInfo.CurrentArea);
            var corpInfo = JsonConvert.DeserializeObject<IList<CompanyInfo>>(corp);
            int cnt = enterpriseServiceClient.GetCompanyCount(queryCondition, UserInfo.CurrentArea);
            ListCorps.AllowCustomPaging = true;
            ListCorps.VirtualItemCount = cnt;
            ListCorps.DataSource = corpInfo;
        }


        protected string GetCorpClassKind(string type)
        {
            string output = string.Empty;
            if (!String.IsNullOrEmpty(type))
            {
                Dictionary<string, string> CorpTypeDic = JsonConvert.DeserializeObject<Dictionary<string, string>>
                    (Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString());
                try
                {
                    output = CorpTypeDic[type];
                }
                catch
                {
                    output = "";
                }
            }
            return output;
        }

        private string GetCondition()
        {
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            QueryCompanyInfoCondition queryCompanyInfoCondition = new QueryCompanyInfoCondition();
            string classId = this.ddlCorpType.SelectedValue;
            string corpName = txtCorpName.Text.Trim();
            List<string> listFlow = GetFlowCodes();
            if (listFlow.Count > 0)
            {
                foreach (var flow in listFlow)
                {
                    QueryCompanyInfoCondition con = new QueryCompanyInfoCondition();
                    int clsId = -1;
                    if (!string.IsNullOrEmpty(classId) && int.TryParse(classId, out clsId))
                    {
                        con.classID = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
                    }
                    if (!string.IsNullOrEmpty(corpName))
                    {
                        con.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
                    }
                    ////解决烟台地区查看外地企业需求,这里说是地区号换字段了，但是前台获取不到变更后的字段，暂时还取以前的账号 2014-01-02 毛凯
                    if (UserInfo.AreaCode.Length >= 4)
                    {
                        con.AreaCode = new ParameterValue<string>() { O = OptEnum.StartWith, V = UserInfo.AreaCode };
                    }
                    con.FlowCode = new ParameterValue<string>() { O = OptEnum.Equal, V = flow };
                    queryCompanyInfoList.Add(con);
                }
            }
            else
            {
                int clsId = -1;
                if (!string.IsNullOrEmpty(classId) && int.TryParse(classId, out clsId))
                {
                    queryCompanyInfoCondition.classID = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
                }
                if (!string.IsNullOrEmpty(corpName))
                {
                    queryCompanyInfoCondition.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
                }
                queryCompanyInfoCondition.FlowCode = new ParameterValue<string>() { O = OptEnum.Like, V = "019" };
                ////解决烟台地区查看外地企业需求,这里说是地区号换字段了，但是前台获取不到变更后的字段，暂时还取以前的账号 2014-01-02 毛凯
                if (UserInfo.AreaCode.Length >= 4)
                {
                    queryCompanyInfoCondition.AreaCode = new ParameterValue<string>() { O = OptEnum.StartWith, V = UserInfo.AreaCode };
                }
                queryCompanyInfoList.Add(queryCompanyInfoCondition);
            }
            return JsonConvert.SerializeObject(queryCompanyInfoList);
        }


        private List<string> GetFlowCodes()
        {
            string flowCode = ddlStatus.SelectedValue;
            List<string> listFlow = new List<string>();
            foreach (var f in flowCode.Split('|'))
            {
                if (!string.IsNullOrEmpty(f))
                {
                    listFlow.Add(f);
                }
            }
            return listFlow;
        }

        private void BindFlowCode()
        {
            if (SystemID.Equals("97") || SystemID.Equals("101"))
            {
                BindFlowCodeForYTLC();
                return;
            }
            string classid = "2502";
            string msg = string.Empty;
        
            Dictionary<string, string> list = new Dictionary<string, string>();
            BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            list = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllBusinessStatus(out msg, AreaCode, classid));
            
            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Key";
            ddlStatus.DataSource = list;
            ddlStatus.DataBind();

            if (list != null && list.Count > 0)
            {
                ddlStatus.SelectedItem.Text = "处理中";
            }
        }

        private void BindFlowCodeForYTLC()
        {
            string classid = "2502";
            string msg = string.Empty;

            Dictionary<string, string> list = new Dictionary<string, string>();
            Dictionary<string, string> listNew = new Dictionary<string, string>();
            BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            list = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllBusinessStatus(out msg, AreaCode, classid));

            foreach (var item in list.Keys)
            {
                if (item.Equals("0"))
                {
                    continue;////主管端不显示未上报的
                }
                string key = item;
                if (key.Contains("@"))
                {
                    key = key.Replace('@', '|');
                }
                listNew.Add(key, list[item]);
            }

            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Key";
            ddlStatus.DataSource = listNew;
            ddlStatus.DataBind();
        }
    }
}